import uuid

import requests
from bs4 import BeautifulSoup
from pymysql import Connection

# 获取正在热映电影的网页源码
def get_movie(headers):
    url = 'http://dianying.taobao.com/showList.htm'
    response = requests.get(url=url, headers=headers)
    return response

# 对热映电影详情进行解析
def parse_detail_movie(url,headers):
    """
    对热映电影详情进行解析
    :param url: 热映电影详细信息的url
    :return: Movie
    """
    response = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(response.text, 'lxml')
    # 获取电影详情信息
    m_id = url.split('&')[0].split('?')[1].split('=')[1]
    m_name = soup.select('.cont-title')[0].get_text().split(' ')[0]
    m_direct = soup.select(".cont-info > li")[0].get_text().split('：')[1]
    m_actors = soup.select(".cont-info > li")[1].get_text().split('：')[1]
    m_type = soup.select(".cont-info > li")[2].get_text().split('：')[1]
    m_area = soup.select(".cont-info > li")[3].get_text().split('：')[1]
    m_date = soup.select('.cont-time')[0].get_text().split('：')[1]
    m_score = soup.select('.detail-cont .score')[0].get_text()
    return Movie(m_id, m_name, m_direct, m_actors, m_type, m_area, m_date, 'None' if m_score == '' else m_score)

# 解析热映电影网页源码
def parse_movie(response,headers):
    """
    对获取的movie页面源码进行解析
    :param response: 页面源码
    :return: (list(Movie),list(showId))
    """
    soup = BeautifulSoup(response.text, 'lxml')
    # 获取每个电影的详细信息的url
    detail_url_tag = soup.select('div[class="tab-movie-list"] > div > a[class="movie-card-buy"]')
    movie_list = list()
    showId_list = list()
    for tag in detail_url_tag:
        # 访问每个电影详细信息并生成Movie对象
        url = tag.attrs.get('href')
        # 保存每部电影的showId
        showId_list.append(url.split('&')[0].split('?')[1].split('=')[1])
        movie = parse_detail_movie(url,headers)
        # 将Movie对象放入list集合
        movie_list.append(movie)
    return (movie_list, showId_list)

# 将电影信息存入数据库
def save_movie_in_mysql(movie_list):
    """
    将电影信息存入数据库
    :param movie_list: 电影对象集合
    :return: None
    """
    # 构建Mysql数据库的连接
    conn = Connection(
        host='localhost',  # 主机名
        port=3306,  # 端口号
        user="root",  # 用户名
        password="admin"  # 密码
    )
    # 获取游标对象
    cursor = conn.cursor()
    # 选择数据库
    conn.select_db("spider")
    # 执行sql
    for movie in movie_list:
        sql = "insert into t_movie values(%s,%s,%s,%s,%s,%s,%s,%s)"
        vale = (
            movie.m_id, movie.m_name, movie.m_director, movie.m_actors, movie.m_type, movie.m_area, movie.m_date,
            movie.m_score)
        cursor.execute(sql, vale)
    # 通过commit提交
    conn.commit()
    # 关闭连接
    conn.close()

class Movie:

    def __init__(self, m_id, m_name, m_director, m_actors, m_type, m_area, m_date, m_score):
        """
        构造方法
        :param m_id: 唯一标识
        :param m_name: 电影名称
        :param m_director: 导演
        :param m_actors: 主演
        :param m_type: 类型
        :param m_area: 地区
        :param m_date: 上映时间
        :param m_score: 评分
        """
        self.m_id = m_id
        self.m_name = m_name
        self.m_director = m_director
        self.m_actors = m_actors
        self.m_type = m_type
        self.m_area = m_area
        self.m_date = m_date
        self.m_score = m_score

    def __str__(self):
        return (self.m_id + "-" + self.m_name + "-" + self.m_director + "-" + self.m_actors + "-" + self.m_type + "-" + self.m_area + '-' + self.m_date + '-' + self.m_score)
